//1、安装ws模块 npm install ws
//2、导入ws模块
const WebSocket = require("ws");
let sendMsg = require("./index.js");
//3、创建WebSocket服务器
const server = new WebSocket.Server({ port: 3000 });
//4、监听open事件
server.on("open", () => {
  console.log("建立连接");
});
//5、绑定close事件
server.on("close", () => {
  console.log("关闭连接");
});
//6、绑定connection事件 ws参数表示客户端 req表示客户端的请求信息
server.on("connection", (ws, req) => {
  //6.1获取客户端的ip port
  const ip = req.connection.remoteAddress;
  const port = req.connection.remotePort;
  const clientName = ip + port;
  console.log("%s is connect", clientName);
  //6.2向客户端发送消息
  ws.send("欢迎" + clientName);
  //监听 客户端 message 事件，转发给其他人
  ws.on("message", (msg) => {
    console.log("消息：%s 来自于 %s", msg, clientName);
    // sendMsg(msg).then((r) => {
    //   console.log(r.text);
    //   ws.send(r.text);
    // });

    // //6.3把消息广播所有客户端：clients表示所有客户端
    server.clients.forEach(function (client) {
      //若某个客户端是打开的，就把消息广播给该客户
      if (client.readyState === WebSocket.OPEN) {
        client.send(clientName + "---------" + msg);
      }
    });
  });
});
// ————————————————
